package com.widget.date;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Objects;

/**
 * 日期工具类
 *
 * @author mlliud
 * @date 2020/7/28 5:04 下午
 */
public class DateHelper {

  /**
   * 定义格式类型
   */
  public static final String STD_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
  public static final String STD_DATE_PATTERN = "yyyy-MM-dd";

  public static final DateTimeFormatter STD_DATE_TIME_FORMAT = DateTimeFormatter.ofPattern(STD_DATE_TIME_PATTERN);
  public static final DateTimeFormatter STD_DATE_FORMAT = DateTimeFormatter.ofPattern(STD_DATE_PATTERN);

  /**
   * 将字符串日期转换为date
   *
   * @param time   字符串日期
   * @param format 格式
   * @return {@link {@link Date}}
   * @throws
   * @author mlliud
   * @date 2020/7/28 5:08 下午
   * @version 1.0
   * @modifyAuthor
   * @modifyDate 2020/7/28 5:08 下午
   */
  public static Date parseStringToDateTime(String time, String format) {
    LocalDateTime localDateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern(format));
    ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
    return Date.from(zdt.toInstant());
  }

  /**
   * 将日期格式化字符串
   *
   * @param date
   * @param pattern
   * @return {@link {@link String}}
   * @throws
   * @author mlliud
   * @date 2020/7/28 5:49 下午
   * @version 1.0
   * @modifyAuthor
   * @modifyDate 2020/7/28 5:49 下午
   */
  public static String formatDate(Date date, String pattern) {
    LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(pattern);
    return timeFormatter.format(localDateTime);
  }

  /**
   * 日期之间相差天数
   *
   * @param start
   * @param end
   * @return {@link {@link int}}
   * @throws
   * @author mlliud
   * @date 2020/7/28 5:09 下午
   * @version 1.0
   * @modifyAuthor
   * @modifyDate 2020/7/28 5:09 下午
   */
  public static int getBetweenDays(Date start, Date end) {
    LocalDate startDate = start.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    LocalDate endDate = end.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    return (int) (ChronoUnit.DAYS.between(startDate, endDate) + 1);
  }

  /**
   * 几天以后日期
   *
   * @param start    开始时间
   * @param afterNum 多少天以后
   * @return {@link {@link Date}}
   * @throws
   * @author mlliud
   * @date 2020/7/28 5:39 下午
   * @version 1.0
   * @modifyAuthor
   * @modifyDate 2020/7/28 5:39 下午
   */
  public static Date afterDate(Date start, int afterNum) {
    LocalDate startDate = start.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    LocalDate localDate = startDate.plusDays(afterNum);
    Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
    return Date.from(instant);
  }

  /**
   * 获取日期最小时间 time 为null默认当前时间
   *
   * @param time 日期
   * @return {@link {@link Date}}
   * @throws
   * @author mlliud
   * @date 2020/7/28 5:41 下午
   * @version 1.0
   * @modifyAuthor
   * @modifyDate 2020/7/28 5:41 下午
   */
  public static Date minToday(Date time) {
    LocalDate localDate;
    if (Objects.isNull(time)) {
      localDate = LocalDate.now();
    } else {
      localDate = time.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }
    LocalDateTime today = LocalDateTime.of(localDate, LocalTime.MIN);
    ZonedDateTime zdt = today.atZone(ZoneId.systemDefault());
    return Date.from(zdt.toInstant());
  }
}
